#include "router.h"

#include <string.h>

#include "log.h"
#include "mqtt_client.h"
#include "thread_pool.h"

#define router_list_size (100)
#define device_subscribe_size (10)
#define device_publish_size (10)

typedef struct {
	const device *device;
	char *subscribe[device_subscribe_size];
} router;

static router router_list[router_list_size];

int32_t router_add(const device *device) {
	if (!device) {
		log_warn("添加设备为空");
		return -1;
	}
	int32_t firest_idle = -1;
	for (int32_t i = 0; i < router_list_size; ++i) {
		if (router_list[i].device == device) {
			log_warn("添加设备已存在");
			return -1;
		} else if (router_list[i].device == NULL) {
			if (firest_idle == -1) {
				firest_idle = i;
			}
		}
	}
	if (firest_idle == -1) {
		log_warn("没有空闲空间");
		return -1;
	}
	router_list[firest_idle].device = device;
	memset(router_list[firest_idle].subscribe, 0,
		   sizeof(router_list->subscribe));
	return 0;
}
int32_t router_subscribe(const device *device, const char *topic) {
	if (!device) {
		log_warn("添加设备为空");
		return -1;
	}
	if (!topic || !strlen(topic)) {
		log_warn("添加主题为空");
	}
	int32_t index = -1;
	for (int32_t i = 0; i < router_list_size; ++i) {
		if (router_list[i].device == device) {
			index = i;
			break;
		}
	}
	if (index == -1) {
		log_warn("此设备未添加，先添加设备");
		return -1;
	}
	char **subscribe = router_list[index].subscribe;
	index = -1;
	int32_t first_idle = -1;
	for (int32_t i = 0; i < device_subscribe_size; ++i) {
		if (subscribe[i] == topic) {
			log_warn("主题已订阅");
			return -1;
		} else if (!subscribe[i]) {
			if (first_idle == -1) {
				first_idle = i;
			}
		} else if (!strcmp(subscribe[i], topic)) {
			index = i;
			break;
		}
	}
	if (index == -1) {
		log_warn("没有空闲空间");
		return -1;
	}
	subscribe[index] = strdup(topic);
	return 0;
}

typedef message _message;

void *router_task(void *pv) {
	message *message = (_message *)pv;
	switch (message->message_type) {
		case device_to_server:
		case server_to_device:
			break;
		default:
			log_warn("消息类型错误");
			goto message_type_error;
	}
	switch (message->connection) {
		case connection_bluetooth:
		case connection_lora:
		case connection_mqtt:
			break;
		default:
			log_warn("消息连接类型错误");
			goto message_type_error;
	}
	if (!message->device_name) {
		log_warn("设备名称为空");
		goto device_name_is_null;
	}
	if (!message->topic) {
		log_warn("主题为空");
		goto topic_is_null;
	}
	if (!message->content) {
		log_warn("内容为空");
		goto content_is_null;
	}
	if (message->message_type == device_to_server) {
		thread_pool_run(mqtt_publish, message);
	} else if (message->message_type == server_to_device) {
		int32_t index = -1;
		router *r = router_list;
		char *device_name = message->device_name;
		for (int32_t i = 0; i < router_list_size; ++i) {
			if (r[i].device && r[i].device->device_name &&
				!strcmp(r[i].device->device_name, device_name)) {
				index = i;
				break;
			}
		}
		if (index == -1) {
			log_warn("没有这个设备");
			goto device_not_found;
		}
		thread_pool_run(r[index].device->write, message);
	}
device_not_found:
unkonw:
content_is_null:
topic_is_null:
device_name_is_null:
message_type_error:
message_type_error:
	return NULL;
}
